package com.ygq.controller;

import DO.TeacherDO;
import DTO.AddTeacherDTO;
import DTO.EditStudentPasswordDTO;
import DTO.TeacherLoginDTO;
import VO.TeacherLoginVO;
import com.ygq.TeacherService;
import constant.CodeConstant;
import constant.JwtClaimsConstant;
import constant.MessageConstant;
import context.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import result.Result;
import lombok.extern.slf4j.Slf4j;
import utils.JwtUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/teacher")
@Slf4j
public class TeacherController {


    @Autowired
    private TeacherService teacherService;

    @Value("${ygq.password}")
    private String password;


    /**
     * 教师登录
     * @param loginBto
     * @return
     */
    @PostMapping("/login")
    public Result<TeacherLoginVO> login(@RequestBody TeacherLoginDTO loginBto){
        // 第一步，查询教师列表是否有这个人
        List<TeacherDO> teachers = teacherService.getTeachers();
        for (TeacherDO teacher : teachers) {
            if (Objects.equals(loginBto.getUsername(), teacher.getUserNumber())){
                // 表示有这个教师账号，接下来进行密码校验
                if (Objects.equals(loginBto.getPassword(), teacher.getPassword())){
                    // 表示密码也可以对上
                    // 生成教师登录token
                    Map<String,Object> claims = new HashMap<>();
                    claims.put(JwtClaimsConstant.OPERATOR, JwtClaimsConstant.TEACHER);
                    claims.put("id",teacher.getId());
                    String token = JwtUtils.generateJwt(claims);
                    TeacherLoginVO teacherLoginVO = new TeacherLoginVO();
                    teacherLoginVO.setTeacher(teacher);
                    teacherLoginVO.setToken(token);
                    return Result.success(teacherLoginVO);
                }
                // 返回密码错误
                return Result.error(MessageConstant.PASSWORD_ERROR, CodeConstant.PASSWORD_ERROR);
            }
        }
        return Result.error(MessageConstant.PASSWORD_ERROR, CodeConstant.PASSWORD_ERROR);
    }


    /**
     * 获取教师列表
     * @return
     */
    @GetMapping("/getTeachers")
    public Result<List<TeacherDO>> getTeachers(){
        List<TeacherDO> teachers = teacherService.getTeachers();
        for (int i = 0; i < teachers.size(); i++) {
            teachers.get(i).setPassword(null);
        }
        return Result.success(teachers);
    }

    /**
     * 重置助手密码
     * @param id
     * @return
     */
    @GetMapping("/resetTeacherPassword")
    public Result<String> resetTeacherPassword(@RequestParam Integer id){
        Integer currentId = BaseContext.getCurrentId();// 操作者id
        Integer teacherId = teacherService.getTeacherByEleve();// 主教师id
        if (!Objects.equals(currentId, teacherId)){
            return Result.error("无权限",201);
        }
        teacherService.resetTeacherPassword(id,password);
        return Result.success("ok");
    }

    /**
     * 删除助手
     * @param id
     * @return
     */
    @GetMapping("/deleteTeacher")
    public Result<String> deleteTeacher(@RequestParam Integer id){
        Integer currentId = BaseContext.getCurrentId();  // 操作者id
        Integer teacherId = teacherService.getTeacherByEleve();  // 主教师id
        if (!Objects.equals(currentId, teacherId)){
            return Result.error("无权限",201);
        }
        if (Objects.equals(id, currentId) || Objects.equals(id,teacherId)){
            // 删除自己 以及 主教师本人，不允许
            return Result.error("无权限",201);
        }
        teacherService.deleteTeacher(id);
        return Result.success("ok");
    }

    /**
     * 添加教师助手
     * @param addTeacherDTO
     * @return
     */
    @PostMapping("/addTeacher")
    public Result<String> addTeacher(@RequestBody AddTeacherDTO addTeacherDTO){
        if (addTeacherDTO.getUsername() ==null || addTeacherDTO.getUserNumber() ==null){
            return Result.error("信息不全",201);
        }
        teacherService.save(addTeacherDTO,password);
        return Result.success("添加成功");
    }


    /**
     * 修改教师端密码
     * @param editStudentPasswordDTO
     * @return
     */
    @PostMapping("/editPassword")
    public Result<String> editPassword(@RequestBody EditStudentPasswordDTO editStudentPasswordDTO){
        editStudentPasswordDTO.setId(BaseContext.getCurrentId());
        return teacherService.updatePassword(editStudentPasswordDTO);
    }
}
